"""tasks固定命名，celery路径里面封装了查询路径"""

# 编写异步任务代码（将要执行的任务redis）
from celery_tasks.sms.yuntongxun.sms import CCP
from . import constants
from celery_tasks.main import celery_app
import logging

logger = logging.getLogger('django')  # 记录发送短信异常日志
# 验证码短信模板(验证码标记)
SMS_CODE_TEMP_ID = 1


@celery_app.task(name='send_sms_code')  # 用celery_app调用task方法来装饰，将send_sms_code加入redis任务队列  name 是任务别名
def send_sms_code(mobile, sms_code):
    try:
        result = CCP().send_template_sms(mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES // 60], SMS_CODE_TEMP_ID)
    except Exception as e:
        logger.error("发送验证码短信[异常][ mobile: %s, message: %s ]" % (mobile, e))
    else:
        if result == 0:
            logger.info("发送验证码短信[正常][ mobile: %s ]" % mobile)
        else:
            logger.warning("发送验证码短信[失败][ mobile: %s ]" % mobile)
